package com.hsbc.dev.teamo4.sms.sm.service.impl;


import com.google.common.base.Preconditions;
import com.hsbc.dev.teamo4.sms.common.utils.data.BeanValidator;
import com.hsbc.dev.teamo4.sms.common.utils.data.LevelUtil;
import com.hsbc.dev.teamo4.sms.common.utils.data.TreeNode;
import com.hsbc.dev.teamo4.sms.common.utils.data.TreeUtil;
import com.hsbc.dev.teamo4.sms.common.utils.factory.UUIDFactory;
import com.hsbc.dev.teamo4.sms.sm.dao.IMenuDao;
import com.hsbc.dev.teamo4.sms.sm.dao.impl.MenuDaoImpl;
import com.hsbc.dev.teamo4.sms.sm.po.Department;
import com.hsbc.dev.teamo4.sms.sm.po.Menu;
import com.hsbc.dev.teamo4.sms.sm.service.IMenuService;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author:Cady
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.service.impl
 * @Description:
 * @Date date: 2018-07-26
 */
public class MenuServiceImpl implements IMenuService {
    private IMenuDao iMenuDao = new MenuDaoImpl();

    /**
     * @Description addMenu
     * @Date: 15:26 2018-07-27
     * @Param menu
     * @return com.hsbc.dev.teamo4.sms.sm.po.Menu
     */
    @Override
    public Menu save(Menu menu) {
        BeanValidator.validateObject(menu);
        if(checkExist(menu.getMenuParentId(),menu.getMenuName(),menu.getId())){
            try {
                throw new Exception("the same level have same menu");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        menu.setMenuLevel(LevelUtil.calculateLevel(getLevel(menu.getMenuParentId()),menu.getMenuOrder()));
        String id= UUIDFactory.getUUID();
        menu.setId(id);
        menu.setCreateId(id);
        menu.setCreateTime(String.valueOf(System.currentTimeMillis()));
        menu.setUpdateId(id);
        menu.setCreateTime(String.valueOf(System.currentTimeMillis()));
        menu.setIsEnable(true);
        return iMenuDao.saveEntity(menu);
    }

    @Override
    public boolean checkExist(String parentId, String departmentName, String departmentId){
        Map map=iMenuDao.countByNameAndParentId(parentId,departmentName,departmentId);
        return map != null;

    }

    /**
     * getLevel
     * @param menuId
     * @return
     */
    private String getLevel(String menuId){
        Menu menu=queryById(menuId);
        if(menu==null){
            return null;
        }
        return menu.getMenuLevel();
    }


    /**
     * @Description updateMenu
     * @Date: 15:26 2018-07-27
     * @Param menu
     * @return com.hsbc.dev.teamo4.sms.sm.po.Menu
     */
    @Override
    public Menu update(Menu menu) {
        BeanValidator.validateObject(menu);
        if(checkExist(menu.getMenuParentId(),menu.getMenuName(),menu.getId())){
            try {
                throw new Exception("the same level have same menu");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Menu beforeMenu=iMenuDao.queryById(menu.getId());
        Preconditions.checkNotNull(beforeMenu,"update menu is not exist");

        menu.setMenuLevel(LevelUtil.calculateLevel(getLevel(menu.getMenuParentId()),menu.getMenuOrder()));
        menu.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        updateWithChild(beforeMenu,menu);
        return menu;
    }

    /**
     * updateWithChild
     * @param beforeMenu
     * @param afterMenu
     */
    private void updateWithChild(Menu beforeMenu,Menu afterMenu){
        String newLevelPrefix=afterMenu.getMenuLevel();
        String oldLevelPrefix=beforeMenu.getMenuLevel();
        if(!afterMenu.getMenuLevel().equals(beforeMenu.getMenuLevel())){
            List<Menu> menuList=iMenuDao.getChildMenuListByLevel(beforeMenu.getMenuLevel());
            if(CollectionUtils.isEmpty(menuList)){
                for (Menu menu:menuList){
                    String level=menu.getMenuLevel();
                    if(level.indexOf(oldLevelPrefix)==0){
                        level=newLevelPrefix+level.substring(oldLevelPrefix.length());
                        menu.setMenuLevel(level);
                    }
                }
                iMenuDao.batchUpdateLevel(menuList);
            }
        }
        iMenuDao.updateEntity(afterMenu);
    }


    /**
     * @Description query Menu By id
     * @Date: 15:26 2018-07-27
     * @Param id
     * @return com.hsbc.dev.teamo4.sms.sm.po.Menu
     */
    @Override
    public Menu queryById(String id) {
        if (id==null|| "".equals(id)) {
            return null;
        }
        return iMenuDao.queryById(id);
    }

    /**
     * @Description query all Menu
     * @Date: 15:27 2018-07-27
     * @Param
     * @return java.util.List<com.hsbc.dev.teamo4.sms.sm.po.Menu>
     */
    @Override
    public List<Menu> queryEntityList() {
        return iMenuDao.queryAllEntity();
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        return iMenuDao.updateByIsEnable(id,bool);
    }

    /**
     * @Description build the menu tree
     * @Date: 15:27 2018-07-27
     * @Param
     * @return com.hsbc.dev.teamo4.sms.common.utils.data.TreeNode<com.hsbc.dev.teamo4.sms.sm.po.Menu>
     */
    @Override
    public TreeNode<Menu> getMenuTree(){
        List<Menu> menus = iMenuDao.queryAllEntity();
        TreeUtil<Menu> treeUtil = new TreeUtil<>(menus,"menuLevel","id","menuParentId");
        return treeUtil.getTree();
    }


    /**
     * @Description set menu.isEnable to 0
     * @Date: 15:28 2018-07-27
     * @Param id
     * @return void
     */
    @Override
    public void deleteMenuById(String id) {
        iMenuDao.deleteMenuById(id);
        /*菜单角色权限的删除*/
    }
}
